package com.etrans.baf.analyse.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlReader {
	public static void main(String[] args) {
		//System.out.println(readXmlFile("D:\\vehicle_swap_exception.xml"));
		Map m = readXmlToMapReduce("D:\\vehicle_swap_track.xml");
		System.out.println(m.get("finalize"));
	}
 
	 
 	public static void readFile(String filePath) {
		String s = null;
		StringBuffer sb = new StringBuffer();

		File file = new File(filePath);
		if (file.exists()) {
			try {
				BufferedReader bufferedReader = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath)));

				try {
					while ((s = bufferedReader.readLine()) != null) {
						sb.append(s);

					}
					System.out.println(sb.toString());
				} catch (IOException e) {
					e.printStackTrace();
				}

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("file not exits!");
		}

	}

	public static String readXmlFile(String filePath) {

		String s = null;
		StringBuffer sb = new StringBuffer();

		String xmlString = null;
		File file = new File(filePath);
		if (file.exists()) {
			try {
				BufferedReader bufferedReader = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath)));

				try {
					while ((s = bufferedReader.readLine()) != null) {
						sb.append(s);

					}
					xmlString = sb.toString();
				} catch (IOException e) {
					e.printStackTrace();
				}

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("file not exits!");
		}
		return xmlString;

	}
	
    /** 
     * 检查字符是否为合法的xml字符 
     * XML规范中规定了允许的字符范围(http://www.w3.org/TR/REC-xml#dt-character):  
     * Char ::= #x9| #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] 
     *  
     * @param ch 
     * @return 
     */  
    public static boolean isLegalXMLCharacter(int ch) {  
        if (ch <= 0xD7FF) {  
            if (ch >= 0x20) {  
                return true;  
            } else {  
                return ch == '\n' || ch == '\r' || ch == '\t';  
            }  
        }else{  
            return (ch >= 0xE000 && ch <= 0xFFFD) || (ch >= 0x10000 && ch <= 0x10FFFF);  
        }  
    }  
    /** 
     * 去掉xml字符串中的非法字符 
     *  
     * @param xml 
     * @return 
     */  
    public static String removeIllegalXmlCharacters(String xml) {  
        if (xml == null || xml.length() <= 0) {  
            return "";  
        }  
        int len = xml.length();  
        StringBuilder sb = new StringBuilder(len);  
        for (int i = 0; i < len; i++) {  
            char ch = xml.charAt(i);  
            if (isLegalXMLCharacter(ch)) {  
                sb.append(ch);  
            }  
        }  
        return sb.toString();  
    }  
    
    public static Map<String,String> readXmlToMapReduce(String xml){
    	Map<String,String> map = new HashMap();
		DocumentBuilder db = null;
		DocumentBuilderFactory dbf = null;
    	try {
    		   File file = new File(xml);
    		   dbf = DocumentBuilderFactory.newInstance();// 返回documentBuilderFactory对象
    		   db = dbf.newDocumentBuilder(); // 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象
    		   Document dt = db.parse(file);// 得到一个DOM并返回给document对象
    		   Element root = dt.getDocumentElement();// 得到一个elment根元素
    		   NodeList childNodes = root.getChildNodes();// 获得根元素下的子节点
    		   for (int i=0;i<childNodes.getLength();i++) {
    			    Node node = childNodes.item(i);
     			    map.put(node.getNodeName(),node.getTextContent().trim());
    		   }
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return map;
    }

}
